package com.exam.controller;

import com.exam.service.ExaminationResultService;
import com.exam.utils.RedisUserIdUtil;
import com.exam.utils.ResultVOUtil;
import com.exam.vo.ResultVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by v
 * Date:2019/2/23
 * Time:21:05
 */
@RestController
@RequestMapping("/examination/result")
@Slf4j
public class ExaminationResultController {

    @Autowired
    private ExaminationResultService examinationResultService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 所有答题记录列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/admin/list")
    public ResultVO examinationResultAllList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                                             HttpServletRequest request) {

        Integer userId = RedisUserIdUtil.getAdminUserId(request,redisTemplate);
        PageInfo<Map<String, Object>> all = examinationResultService.getAllList(page, size);

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("number", all.getTotal());
        map.put("currentPage", page);
        map.put("data", all.getList());
        return ResultVO.success(map);
    }

    /**
     * 个人答题记录列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/user/list")
    public ResultVO examinationResultSelfList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                                              HttpServletRequest request) {

        PageInfo<Map<String, Object>> all = examinationResultService.getSelfList(request,page, size);

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("number", all.getTotal());
        map.put("currentPage", page);
        map.put("data", all.getList());
        return ResultVO.success(map);
    }


    @GetMapping("/detail")
    public ResultVO examinationDetail(@RequestParam(value = "examinationResultId") Integer examinationResultId) {

        return ResultVOUtil.success(examinationResultService.getResultDetail(examinationResultId));
    }
}
